import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Slf4j
public class ThreadLocalSocketServer {


    public static void main(String[] args) throws IOException {
        ExecutorService executorService = Executors.newFixedThreadPool(40);

        ServerSocket serverSocket = new ServerSocket(8081);

        while (true) {
            Socket socket = serverSocket.accept();
            executorService.execute(() -> {
                sendHttpContent(socket);
            });
        }
    }

    public static void sendHttpContent(Socket socket) {

        try {
            OutputStream outputStream = socket.getOutputStream();

            String content = "Hello World my custom socket server";

            PrintWriter writer = new PrintWriter(outputStream);
            writer.println("HTTP/1.1 200 OK");
            writer.println("Server: Nginx");
            writer.println("Content-Type: text/html;charset=UTF-8");
            writer.println("Content-Length: " + content.length());
            writer.println("Connection: keep-alive");
            writer.println("");
            writer.println(content);

            writer.flush();
            writer.close();

            socket.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
